package com.gdlgxy.oa.controller;

import com.gdlgxy.oa.bean.Student;
import com.gdlgxy.oa.bean.StudentList;
import com.gdlgxy.oa.service.StudentServer;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

public class StudentUpdateCtrl extends HttpServlet {

    /**
     * 修改的学生链表
     */
    private StudentList list;
    /**
     * 要修改的学生节点
     */
    private Student temp;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取动态应用的根路径
        String contextPath = request.getContextPath();

        // 设置响应的内容类型，防止中文乱码问题
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.print(" <!DOCTYPE html>");
        out.print(" <html lang='zh-CN'>");
        out.print(" <head>");
        out.print("     <meta charset='UTF-8'>");
        out.print("     <meta http-equiv='X-UA-Compatible' content='IE=edge'>");
        out.print("     <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.print("     <title>修改学生</title>");
        out.print("     <style>");
        out.print("                 h1 {");
        out.print("             padding: 0px 20px;");
        out.print("             border-left: 10px solid #ed515191;");
        out.print("             background-color: rgba(208, 208, 208, 0);");
        out.print("             font-size: 19px;");
        out.print("             line-height: 30px;");
        out.print("             color: cornflowerblue;");
        out.print("             font-weight: bold;");
        out.print("             margin: 5px 0;");
        out.print("         }");
        out.print("         li {");
        out.print("             list-style: none;");
        out.print("         }");
        out.print("         form {");
        out.print("             display: block;");
        out.print("             width: 300px;");
        out.print("             margin: auto;");
        out.print("         }");
        out.print("         ul.IsignUp li {");
        out.print("             margin-top: 5px;");
        out.print("         }");
        out.print("         input.inputLine {");
        out.print("             width: 226px;");
        out.print("             padding: .375rem .75rem;");
        out.print("             border-radius: .25rem;");
        out.print("             border: 1px solid #ced4da;");
        out.print("         }");
        out.print("         input.inputLine:focus {");
        out.print("             border-style: solid;");
        out.print("             border-color: #03a9f4;");
        out.print("             box-shadow: 0 0 5px #03a9f4;");
        out.print("         }");
        out.print("         input.inputLeft {");
        out.print("             width: 130px;");
        out.print("         }");
        out.print("         button, select {");
        out.print("             padding: .375rem .75rem;");
        out.print("             border-radius: .25rem;");
        out.print("             border: 1px solid #ced4da;");
        out.print("         }");
        out.print("         button:hover, select:hover {");
        out.print("             border-color: #03a9f4;");
        out.print("         }");
        out.print("         button:active {");
        out.print("             background: #03a9f4;");
        out.print("         }");
        out.print("     </style>");
        out.print(" <body>");
        out.print(" <div class='test'>");
        out.print("     <h1>修改学生界面</h1>");
        out.print(" </div>");
        // 以上是固定的

        // 获取学生编号
        int sno = Integer.parseInt(request.getParameter("Sno"));
        // 获取学生链表
        list = StudentServer.list();
        // 根据编号查询的学生姓名
        temp = list.studentFind(sno);

        out.print(" <form action='" + contextPath + "/student/update' method='post'>");
        out.print("     <ul class='IsignUp' style='padding:0px'>");
        out.print("         <li><label for='name'>学生姓名：</label>");
        out.print("             <input class='inputLeft inputLine' type='text' id='name' name='Sname' value='" + temp.getName() + "'>");


        // 以下是固定的
        out.print("         </li>");
        out.print("         <li>");
        out.print("             <button class='inputLine' type='submit' class='i-button' onclick='addOneToOne()'>");
        out.print("                 确认修改");
        out.print("                 </button>");
        out.print("         </li>");
        out.print("     </ul>");
        out.print(" </form>");
        out.print(" </body>");
        out.print(" </html>        ");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 注意乱码问题
        request.setCharacterEncoding("UTF-8");

        // 获取修改的学生编号与姓名
        String sname = request.getParameter("Sname");

        System.out.println("controller：学生编号为："+temp.getNo()+"，修改前的学生姓名为：" + temp.getName() + ",修改后的学生姓名为：" + sname);
        // 直接修改节点
        temp.setName(sname);

        // 向server层传递修改后的单向链表
        // 向数据库传递修改后链表
        // 修改成功就转发回学生链表
        if (StudentServer.update(list)) {
            request.getRequestDispatcher("/student/list").forward(request, response);
        } else {
            // 删除失败
            request.getRequestDispatcher("/error.html").forward(request, response);
        }
    }
}
